
/* 
    @fiel:  gui_keyboard_lv.c
    @code:  c(//utf-8)
 */
/* ------------------------------------------------------------------------- */
#include "gui_keyboard_lv.h"
#include "stdio.h"


#define         KB_KEY_EMPTY        0x00    //no function
#define         KB_KEY_ENTER        0x0d    //ok
#define         KB_KEY_BS           0x08    //backspace
#define         KB_KEY_ESC          0x1b    //cancel input

#define         KB_KEY_ABC          0x81    //change to abc 
#define         KB_KEY_NUM          0x82    //change to num
#define         KB_KEY_CPL          0x83    //Caps Lock
#define         KB_KEY_UPABC        0x84    //UP ABC
#define         KB_KEY_DNABC        0x85    //down ABC


typedef struct
{
    unsigned char       keyid;
    unsigned int        lx;
    unsigned int        ly;
    unsigned int        sx;
    unsigned int        sy;
    unsigned int        type;
}key_info_t;


key_user_data_t     kb_keys[50];


key_info_t          num_kb_keys[16]=
{
    {'1',65,189,145,57,1},{'2',240,189,145,57,1},{'3',415,189,145,57,1},{KB_KEY_BS,590,189,145,57,0},
    {'4',65,262,145,57,1},{'5',240,262,145,57,1},{'6',415,262,145,57,1},{KB_KEY_ESC,590,262,145,57,0},
    {'7',65,335,145,57,1},{'8',240,335,145,57,1},{'9',415,335,145,57,1},{KB_KEY_ABC,590,335,145,57,0},
    {'-',65,407,145,57,1},{'0',240,407,145,57,1},{'.',415,407,145,57,1},{KB_KEY_ENTER,590,407,145,57,0},
};

#define        SABCKB_NUMBER        31
key_info_t          abc_kb_keys[SABCKB_NUMBER]=
{
    {'q',55,189,60,57,1},{'w',125,189,60,57,1},{'e',195,189,60,57,1},{'r',265,189,60,57,1},{'t',335,189,60,57,1},
    {'y',405,189,60,57,1},{'u',475,189,60,57,1},{'i',545,189,60,57,1},{'o',615,189,60,57,1},{'p',685,189,60,57,1},

    {'a',85,262,60,57,1},{'s',155,262,60,57,1},{'d',225,262,60,57,1},{'f',295,262,60,57,1},{'g',365,262,60,57,1},
    {'h',435,262,60,57,1},{'j',505,262,60,57,1},{'k',575,262,60,57,1},{'l',645,262,60,57,1},

    {KB_KEY_CPL,55,335,92,57,0},{'z',155,335,60,57,1},{'x',225,335,60,57,1},{'c',295,335,60,57,1},{'v',365,335,60,57,1},
    {'b',435,335,60,57,1},{'n',505,335,60,57,1},{'m',575,335,60,57,1},{KB_KEY_BS,645,335,92,57,0},

    {' ',295,407,197,57,1},
    {KB_KEY_NUM,55,407,92,57,0},{KB_KEY_ENTER,645,407,92,57,0},
};

unsigned char kb_backimg_bf[800*480*2];
lv_img_dsc_t kb_backimg = {
  .header.always_zero = 0,
  .header.w = 800,
  .header.h = 480,
  .data_size = 800*480*2,
  .header.cf = LV_IMG_CF_TRUE_COLOR,
  .data = kb_backimg_bf,
};
void kb_backimginit(void)
{
    int i;
    for ( i = 0; i < (800*480); i++)
    {
        kb_backimg_bf[i*2] = 0xff;
        kb_backimg_bf[i*2+1] = 0xff;
    }
}
static lv_style_t ui_kbt0_btn_release;//按钮释放状态
static lv_style_t ui_kbt01_btn_press;//按钮按下状态
static lv_style_t ui_kbt1_btn_release;//按钮释放状态

static lv_style_t ui_kb_label_style24_b;//24大小黑色
/* ------------------------------------------------------------------------- */


void munkb_map(gui_keyboard_t * pkeyboard);
void abckb_map(gui_keyboard_t * pkeyboard);

/* ------------------------------------------------------------------------- */

//static lv_style_t style_mbox_bg;        /*Black bg. style with opacity*/

unsigned int getstrlen(unsigned char * pstr)
{
    unsigned int i = 0;
    for ( i = 0; i < 40000; i++)
    {
        if(!pstr[i])
        {
            return i;
        }
    }
    
    return i;
}

void ui_kb_addch(gui_keyboard_t * pkeyboard,unsigned char ch)
{
    unsigned int idx;
    unsigned int i;

    idx = getstrlen(pkeyboard->pstr);
    if(idx>pkeyboard->strlen)
    {
        idx = pkeyboard->strlen;
    }

    if(pkeyboard->pstr)
    {
        if(idx<(pkeyboard->strlen-1))
        {
            pkeyboard->pstr[idx] = ch;
            pkeyboard->pstr[idx+1] = 0;
            if(pkeyboard->psden)
            {
                if(pkeyboard->pstrtmp)
                {
                    for ( i = 0; i < idx; i++)
                    {
                        pkeyboard->pstrtmp[i] = '*';
                    }
                    pkeyboard->pstrtmp[idx] = ch;
                    pkeyboard->pstrtmp[idx+1] = 0;

                    
                    lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstrtmp);
                }
                else
                {
                    lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
                }
            }
            else
            {
                lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
            }
        }
    }
}
void ui_kb_delone(gui_keyboard_t * pkeyboard)
{
    unsigned int idx;
    unsigned int i;
    
    idx = getstrlen(pkeyboard->pstr);
    if(idx>pkeyboard->strlen)
    {
        idx = pkeyboard->strlen;
    }

    if(pkeyboard->pstr)
    {
        if((idx<(pkeyboard->strlen))&&(idx>0))
        {
            pkeyboard->pstr[idx-1] = 0;
            if(pkeyboard->psden)
            {
                if(pkeyboard->pstrtmp)
                {
                    for ( i = 0; i < (idx-1); i++)
                    {
                        pkeyboard->pstrtmp[i] = '*';
                    }
                    pkeyboard->pstrtmp[idx-1] = 0;

                    
                    lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstrtmp);
                }
                else
                {
                    lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
                }
            }
            else
            {
                lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
            }
        }
    }
}


static void ui_kb_key_event(lv_obj_t * btn, lv_event_t event)
{
    key_user_data_t * key_info;
    if(event == LV_EVENT_RELEASED)
    {
        if(btn->user_data)
        {
            key_info = (key_user_data_t *)btn->user_data;

            if((key_info->keyid>=0x20)&&(key_info->keyid<=0x7e))
            {
                ui_kb_addch(key_info->pkeyboard,key_info->keyid);
            }

            if(key_info->keyid == KB_KEY_ESC)
            {
                lv_obj_del(key_info->pkeyboard->wp);
                key_info->pkeyboard->wp = NULL;

                //lv_obj_set_style(lv_disp_get_layer_top(NULL), &lv_style_transp);
                lv_obj_set_click(lv_disp_get_layer_top(NULL), false);
                
                key_info->pkeyboard->res = 0;
            }

            if(key_info->keyid == KB_KEY_ENTER)
            {
                lv_obj_del(key_info->pkeyboard->wp);
                key_info->pkeyboard->wp = NULL;

                //lv_obj_set_style(lv_disp_get_layer_top(NULL), &lv_style_transp);
                lv_obj_set_click(lv_disp_get_layer_top(NULL), false);
                
                key_info->pkeyboard->res = 1;
            }

            if(key_info->keyid == KB_KEY_BS)
            {
                ui_kb_delone(key_info->pkeyboard);
            }

            if(key_info->keyid == KB_KEY_ABC)
            {
                gui_keyboard_t * pkeyboard = key_info->pkeyboard;
                lv_obj_del(key_info->pkeyboard->wp);
                key_info->pkeyboard->wp = NULL;
                abckb_map(pkeyboard);

            }

            if(key_info->keyid == KB_KEY_NUM)
            {
                gui_keyboard_t * pkeyboard = key_info->pkeyboard;
                lv_obj_del(key_info->pkeyboard->wp);
                key_info->pkeyboard->wp = NULL;
                munkb_map(pkeyboard);

            }

        }
    }
}


void munkb_map(gui_keyboard_t * pkeyboard)
{
    lv_obj_t * obj_kb;
    lv_obj_t * obj_kb_label;
    int i;

    /* back up */
                            pkeyboard->wp = lv_img_create(lv_disp_get_layer_top(NULL), NULL);
    lv_img_set_src(         pkeyboard->wp, &kb_backimg);
    lv_obj_set_pos(         pkeyboard->wp,0,0);

    //lv_obj_set_style(lv_disp_get_layer_top(NULL), &style_mbox_bg);
    lv_obj_set_click(lv_disp_get_layer_top(NULL), true);

                            pkeyboard->strlabel = lv_label_create(pkeyboard->wp,NULL);
    lv_obj_set_pos(         pkeyboard->strlabel,100,95);
    if(pkeyboard->psden)
    {
        if(pkeyboard->pstrtmp)
        {
            lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstrtmp);
        }
        else
        {
            lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
        }
    }
    else
    {
        lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
    }
    lv_label_set_style(     pkeyboard->strlabel,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//



    for ( i = 0; i < 16; i++)
    {
        kb_keys[i].keyid = num_kb_keys[i].keyid;
        kb_keys[i].pkeyboard = pkeyboard;

                                obj_kb = lv_btn_create(pkeyboard->wp, NULL);
        lv_obj_set_size(        obj_kb,num_kb_keys[i].sx,num_kb_keys[i].sy);//
        lv_obj_set_pos(         obj_kb,num_kb_keys[i].lx,num_kb_keys[i].ly);//设置坐标
        lv_btn_set_layout(      obj_kb,LV_LAYOUT_CENTER);
        if(num_kb_keys[i].type)
        {
            lv_btn_set_style(       obj_kb,LV_BTN_STYLE_REL,&ui_kbt1_btn_release);
        }
        else
        {
            lv_btn_set_style(       obj_kb,LV_BTN_STYLE_REL,&ui_kbt0_btn_release);
        }
        lv_btn_set_style(       obj_kb,LV_BTN_STYLE_PR,&ui_kbt01_btn_press);
        lv_obj_set_user_data(   obj_kb,(lv_obj_user_data_t)&kb_keys[i]);
        lv_obj_set_event_cb(    obj_kb, ui_kb_key_event);

        if((kb_keys[i].keyid>=0x20)&&(kb_keys[i].keyid<=0x7e))
        {
            char strtmp[2];
            strtmp[0] = kb_keys[i].keyid;
            strtmp[1] = 0;
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,strtmp);
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }


        if(kb_keys[i].keyid == KB_KEY_ENTER)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"OK");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_BS)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"DEL");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_ESC)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"Esc");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_ABC)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"ABC");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }
        

    }
    
}   
void abckb_map(gui_keyboard_t * pkeyboard)
{
    lv_obj_t * obj_kb;
    lv_obj_t * obj_kb_label;
    int i;

    /* back up */
                            pkeyboard->wp = lv_img_create(lv_disp_get_layer_top(NULL), NULL);
    lv_img_set_src(         pkeyboard->wp, &kb_backimg);
    lv_obj_set_pos(         pkeyboard->wp,0,0);

    //lv_obj_set_style(lv_disp_get_layer_top(NULL), &style_mbox_bg);
    lv_obj_set_click(lv_disp_get_layer_top(NULL), true);

                            pkeyboard->strlabel = lv_label_create(pkeyboard->wp,NULL);
    lv_obj_set_pos(         pkeyboard->strlabel,100,95);
    if(pkeyboard->psden)
    {
        if(pkeyboard->pstrtmp)
        {
            lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstrtmp);
        }
        else
        {
            lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
        }
    }
    else
    {
        lv_label_set_text(      pkeyboard->strlabel,pkeyboard->pstr);
    }
    lv_label_set_style(     pkeyboard->strlabel,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//



    for ( i = 0; i < SABCKB_NUMBER; i++)
    {
        kb_keys[i].keyid = abc_kb_keys[i].keyid;
        kb_keys[i].pkeyboard = pkeyboard;

                                obj_kb = lv_btn_create(pkeyboard->wp, NULL);
        lv_obj_set_size(        obj_kb,abc_kb_keys[i].sx,abc_kb_keys[i].sy);//
        lv_obj_set_pos(         obj_kb,abc_kb_keys[i].lx,abc_kb_keys[i].ly);//设置坐标
        lv_btn_set_layout(      obj_kb,LV_LAYOUT_CENTER);
        if(abc_kb_keys[i].type)
        {
            lv_btn_set_style(       obj_kb,LV_BTN_STYLE_REL,&ui_kbt1_btn_release);
        }
        else
        {
            lv_btn_set_style(       obj_kb,LV_BTN_STYLE_REL,&ui_kbt0_btn_release);
        }
        lv_btn_set_style(       obj_kb,LV_BTN_STYLE_PR,&ui_kbt01_btn_press);
        lv_obj_set_user_data(   obj_kb,(lv_obj_user_data_t)&kb_keys[i]);
        lv_obj_set_event_cb(    obj_kb, ui_kb_key_event);

        if((kb_keys[i].keyid>=0x20)&&(kb_keys[i].keyid<=0x7e))
        {
            char strtmp[2];
            strtmp[0] = kb_keys[i].keyid;
            strtmp[1] = 0;
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,strtmp);
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }


        if(kb_keys[i].keyid == KB_KEY_ENTER)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"OK");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_BS)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"DEL");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_ESC)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"Esc");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

        if(kb_keys[i].keyid == KB_KEY_ABC)
        {
                                    obj_kb_label =lv_label_create(obj_kb,NULL);
            lv_label_set_text(      obj_kb_label,"ABC");
            lv_label_set_style(     obj_kb_label,LV_LABEL_STYLE_MAIN,&ui_kb_label_style24_b);//
        }

    }
    
}   

unsigned char ui_getstring(gui_keyboard_t * pkeyboard)
{
    kb_backimginit();
    
    // lv_style_copy(&style_mbox_bg, &lv_style_plain);
    // style_mbox_bg.body.main_color = LV_COLOR_BLACK;
    // style_mbox_bg.body.grad_color = LV_COLOR_BLACK;
    // style_mbox_bg.body.opa = LV_OPA_50;

    /* 按键样式初始化 */
    lv_style_copy(&ui_kbt0_btn_release,&lv_style_plain_color); //设置纯色的背景
    ui_kbt0_btn_release.body.main_color = LV_COLOR_MAKE(0x5c,0x66,0x6f); 
    ui_kbt0_btn_release.body.grad_color = ui_kbt0_btn_release.body.main_color;
    ui_kbt0_btn_release.body.opa = LV_OPA_COVER;//设置背景色完全不透明
    ui_kbt0_btn_release.body.radius = 10;//绘制圆角按钮
    ui_kbt0_btn_release.body.shadow.color = LV_COLOR_MAKE(0x5c,0x66,0x6f); 
    ui_kbt0_btn_release.body.shadow.type = LV_SHADOW_FULL;//设置四边都有阴影
    ui_kbt0_btn_release.body.shadow.width = 2;//设置阴影的宽度
    ui_kbt0_btn_release.text.color = LV_COLOR_WHITE; 
    ui_kbt0_btn_release.body.padding.left = 5;//设置左内边距
    ui_kbt0_btn_release.body.padding.right = 5;//设置右内边距


    lv_style_copy(&ui_kbt01_btn_press,&lv_style_plain_color); //设置纯色的背景
    ui_kbt01_btn_press.body.main_color = LV_COLOR_MAKE(0xc1,0x27,0x2d); 
    ui_kbt01_btn_press.body.grad_color = ui_kbt01_btn_press.body.main_color;
    ui_kbt01_btn_press.body.opa = LV_OPA_COVER;//设置背景色完全不透明
    ui_kbt01_btn_press.body.radius = 10;//绘制圆角按钮
    ui_kbt01_btn_press.body.shadow.color = LV_COLOR_MAKE(0xc1,0x27,0x2d); 
    ui_kbt01_btn_press.body.shadow.type = LV_SHADOW_FULL;//设置四边都有阴影
    ui_kbt01_btn_press.body.shadow.width = 2;//设置阴影的宽度
    ui_kbt01_btn_press.text.color = LV_COLOR_WHITE; 
    ui_kbt01_btn_press.body.padding.left = 5;//设置左内边距
    ui_kbt01_btn_press.body.padding.right = 5;//设置右内边距

    lv_style_copy(&ui_kbt1_btn_release,&lv_style_plain_color); //设置纯色的背景
    ui_kbt1_btn_release.body.main_color = LV_COLOR_MAKE(0xC9,0xC9,0xC9); 
    ui_kbt1_btn_release.body.grad_color = ui_kbt1_btn_release.body.main_color;
    ui_kbt1_btn_release.body.opa = LV_OPA_COVER;//设置背景色完全不透明
    ui_kbt1_btn_release.body.radius = 10;//绘制圆角按钮
    ui_kbt1_btn_release.body.shadow.color = LV_COLOR_MAKE(0xC9,0xC9,0xC9); 
    ui_kbt1_btn_release.body.shadow.type = LV_SHADOW_FULL;//设置四边都有阴影
    ui_kbt1_btn_release.body.shadow.width = 2;//设置阴影的宽度
    ui_kbt1_btn_release.text.color = LV_COLOR_WHITE; 
    ui_kbt1_btn_release.body.padding.left = 5;//设置左内边距
    ui_kbt1_btn_release.body.padding.right = 5;//设置右内边距

    lv_style_copy(&ui_kb_label_style24_b,&lv_style_plain_color);//样式拷贝
    //ui_kb_label_style24_b.text.font = &tt_font244;//设置字体
    ui_kb_label_style24_b.text.font = &lv_font_roboto_28;
    ui_kb_label_style24_b.text.color = LV_COLOR_BLACK;


    
    if(pkeyboard->kbtype == 0)
    {
        munkb_map(pkeyboard);
    }
    else
    {
        abckb_map(pkeyboard);
    }
}

